package com.nf.health.app.activity.home.adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.nf.health.app.R;
import com.nf.health.app.activity.home.NewWebViewActivity;
import com.nf.health.app.activity.home.bean.InformationNotice;
import com.nf.health.app.adapter.OnMyClickListener;
import com.nf.health.app.glide.GlideUtil;
import com.nf.health.app.models.WebModel;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.DateUtil;

import java.util.List;

/**
 * 信息公告列表
 */

public class InformationNoticeAdapter extends BaseAdapter{

    private Context context;
    private List<InformationNotice> list;
    public InformationNoticeAdapter(Context context, List<InformationNotice> list) {
        super();
        this.context = context;
        this.list = list;
    }
    @Override
    public int getCount() {
        if(list != null){
            return list.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView==null) {
            convertView = View.inflate(context, R.layout.item_informationnotice,null);
            holder = new ViewHolder();
            holder.iv_information_img = (ImageView) convertView.findViewById(R.id.iv_information_img);
            holder.tv_inmformation_title = (TextView) convertView.findViewById(R.id.tv_inmformation_title);
            holder.tv_information_content = (TextView) convertView.findViewById(R.id.tv_information_content);
            holder.tv_information_bgks = (TextView) convertView.findViewById(R.id.tv_information_bgks);
            holder.tv_information_timer = (TextView) convertView.findViewById(R.id.tv_information_timer);
            holder.ll_information_mnotice = (LinearLayout) convertView.findViewById(R.id.ll_information_mnotice);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final InformationNotice informationNotice = list.get(position);
        GlideUtil.loadNetImgC(context, holder.iv_information_img, informationNotice.getHimg(), R.drawable.default_image);
        holder.tv_inmformation_title.setText(informationNotice.getTitle());
        holder.tv_information_content.setText(informationNotice.getOutline()+"");
        long timer = informationNotice.getCreatetime();
        String t = DateUtil.date2String(timer, "yyyy-MM-dd HH:mm:ss");
        holder.tv_information_timer.setText(t);
        holder.tv_information_bgks.setText(informationNotice.getTypename());
        holder.ll_information_mnotice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                WebModel webModel = new WebModel();
                webModel.setLoadHtmlLoadcalUrl(false);
                webModel.setUrl(informationNotice.getTourl());
                bundle.putSerializable(WebModel.WEBFLAG_TAG, webModel);
                ActivityUtils.showActivity(context, NewWebViewActivity.class, bundle);
            }
        });
        return convertView;
    }
    private OnMyClickListener listener;
    public void setOnMyClickListener(OnMyClickListener listener){
        this.listener = listener;
    }

    public void setMynotifyDataChanged(List<InformationNotice> list) {
        this.list =list;
        notifyDataSetChanged();
    }


    public class ViewHolder{
        ImageView iv_information_img;
        TextView tv_inmformation_title;
        TextView tv_information_content;
        TextView tv_information_bgks;
        TextView tv_information_timer;
        LinearLayout ll_information_mnotice;
    }
}
